<script setup lang="ts">
import { computed, onMounted, reactive, ref, watch } from 'vue';
import { $t } from '@/locales';
import { type SmsLog, fetchSmsChannelOptions } from '@/service/api';
import DictTag from '@/components/custom/dict-tag/src/dict-tag.vue';
import { DICT_TYPE } from '@/utils/dict';

defineOptions({
  name: 'SmsLogDetailModel'
});

interface Props {
  /** the edit row data */
  rowData?: SmsLog | null;
}

const props = defineProps<Props>();

const visible = defineModel<boolean>('visible', {
  default: false
});

const title = computed(() => {
  return $t('common.detail');
});

const model: Model = reactive(createDefaultModel());

const channelList = ref<CommonType.OptionExtra<number>[] | null>([]); // 短信渠道列表

type Model = Pick<
  SmsLog,
  | 'id'
  | 'channelId'
  | 'channelCode'
  | 'templateId'
  | 'templateCode'
  | 'templateType'
  | 'templateContent'
  | 'templateParams'
  | 'apiTemplateId'
  | 'mobile'
  | 'userId'
  | 'userType'
  | 'sendStatus'
  | 'sendTime'
  | 'apiSendCode'
  | 'apiSendMsg'
  | 'apiRequestId'
  | 'apiSerialNo'
  | 'receiveStatus'
  | 'receiveTime'
  | 'apiReceiveCode'
  | 'apiReceiveMsg'
  | 'createTime'
>;

function createDefaultModel(): Model {
  return {
    id: 0,
    channelId: null,
    channelCode: '',
    templateId: '',
    templateCode: '',
    templateType: '',
    templateContent: '',
    templateParams: null,
    apiTemplateId: '',
    mobile: '',
    userId: null,
    userType: null,
    sendStatus: '',
    sendTime: null,
    apiSendCode: '',
    apiSendMsg: '',
    apiRequestId: '',
    apiSerialNo: '',
    receiveStatus: '',
    receiveTime: null,
    apiReceiveCode: '',
    apiReceiveMsg: '',
    createTime: ''
  };
}

function handleInitModel() {
  Object.assign(model, createDefaultModel());

  if (props.rowData) {
    Object.assign(model, props.rowData);
  }
}

function closeDrawer() {
  visible.value = false;
}

watch(visible, () => {
  if (visible.value) {
    handleInitModel();
  }
});

onMounted(async () => {
  // 加载渠道列表
  const { data: res } = await fetchSmsChannelOptions();
  channelList.value = res;
});
</script>

<template>
  <NModal v-model:show="visible" :title="title" preset="card" class="w-250">
    <NScrollbar class="max-h-200 pr-20px">
      <NDescriptions :column="1" label-placement="left" bordered>
        <NDescriptionsItem :label="$t('page.base.sms.log.id')">
          {{ model.id }}
        </NDescriptionsItem>
        <NDescriptionsItem :label="$t('page.base.sms.log.channel')">
          {{ channelList?.find(channel => channel.value === model.channelId)?.extra }}
          <DictTag :type="DICT_TYPE.SYSTEM_SMS_CHANNEL_CODE" :value="model.channelCode" />
        </NDescriptionsItem>
        <NDescriptionsItem :label="$t('page.base.sms.log.template')">
          {{ model.templateId }} | {{ model.templateCode }}
          <DictTag :type="DICT_TYPE.SYSTEM_SMS_TEMPLATE_TYPE" :value="model.templateType.toString()" />
        </NDescriptionsItem>
        <NDescriptionsItem :label="$t('page.base.sms.log.apiTemplateId')">
          {{ model.apiTemplateId }}
        </NDescriptionsItem>
        <NDescriptionsItem :label="$t('page.base.sms.log.userInfo')">
          {{ model.mobile }}
          <span v-if="model.userType && model.userId">
            <DictTag :type="DICT_TYPE.USER_TYPE" :value="model.userType" />
            ({{ model.userId }})
          </span>
        </NDescriptionsItem>
        <NDescriptionsItem :label="$t('page.base.sms.log.templateContent')">
          {{ model.templateContent }}
        </NDescriptionsItem>
        <NDescriptionsItem :label="$t('page.base.sms.log.templateParams')">
          {{ model.templateParams }}
        </NDescriptionsItem>
        <NDescriptionsItem :label="$t('page.base.sms.log.createTime')">
          {{ model.createTime }}
        </NDescriptionsItem>
        <NDescriptionsItem :label="$t('page.base.sms.log.sendStatus')">
          <DictTag :type="DICT_TYPE.SYSTEM_SMS_SEND_STATUS" :value="model.sendStatus.toString()" />
        </NDescriptionsItem>
        <NDescriptionsItem :label="$t('page.base.sms.log.sendTime')">
          {{ model.sendTime }}
        </NDescriptionsItem>
        <NDescriptionsItem :label="$t('page.base.sms.log.apiSendMsg')">
          {{ model.apiSendCode }} | {{ model.apiSendMsg }}
        </NDescriptionsItem>
        <NDescriptionsItem :label="$t('page.base.sms.log.apiSerialNo')">
          {{ model.apiSerialNo }}
        </NDescriptionsItem>
        <NDescriptionsItem :label="$t('page.base.sms.log.apiRequestId')">
          {{ model.apiRequestId }}
        </NDescriptionsItem>
        <NDescriptionsItem :label="$t('page.base.sms.log.receiveStatus')">
          <DictTag :type="DICT_TYPE.SYSTEM_SMS_RECEIVE_STATUS" :value="model.receiveStatus.toString()" />
          {{ model.receiveTime }}
        </NDescriptionsItem>
        <NDescriptionsItem :label="$t('page.base.sms.log.apiReceiveMsg')">
          {{ model.apiReceiveCode }} | {{ model.apiReceiveMsg }}
        </NDescriptionsItem>
      </NDescriptions>
    </NScrollbar>
    <template #footer>
      <NSpace justify="end" :size="16">
        <NButton @click="closeDrawer">{{ $t('common.close') }}</NButton>
      </NSpace>
    </template>
  </NModal>
</template>

<style scoped></style>
